﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using SharePointBrowser.Properties;
using Microsoft.WindowsMobile.PocketOutlook;
using SharePointBrowser.WebsWS;
using System.Net;
using System.Xml;
using SharePointBrowser.SiteDataWS;
using SharePointBrowser.ListsWS;
using SharePointBrowser.Forms;
using SharePointBrowser.Data;

namespace SharePointBrowser
{
    public class ListList : SlidingListControl
    {
        public event EventHandler RootFolderReached;

        private Stack<string> folders = new Stack<string>();
        private string listId;
        private string siteUrl;
        private string listName;

        public string ListId
        {
            get
            {
                return listId;
            }
            set
            {
                listId = value;
                folders.Clear();
            }
        }

        public string SiteUrl 
        {
            get
            {
                return siteUrl;
            }
            set
            {
                siteUrl = value;
                folders.Clear();
            }
        }

        public string ListName
        {
            get
            {
                return listName;
            }
            set
            {
                listName = value;
                folders.Clear();
            }
        }

        public bool AtRootFolder
        {
            get { return folders.Count == 1; }
        }

        public ListList() { }

        public override void LoadData()
        {
            int width = Width;
            int height = Height;

            System.Threading.Thread asyncLoading = new System.Threading.Thread(delegate()
            {
                Invoke(new UIThreadDelegate(ShowWaitingCursor));
                _IsDataLoading = true;

                int y = 0;

                if (folders.Count == 0)
                {
                    XmlNode list = SPAdapter.Instance.GetList(ListName, SiteUrl);
                    folders.Push(list.Attributes["RootFolder"].Value.Trim('/'));
                }
                List<string> path = new List<string>(folders);
                path.Reverse();
                XmlNode listItemsXml = SPAdapter.Instance.GetListItems(ListId, string.Join("/", path.ToArray()), SiteUrl);

                internalDataList = new List<ISlidingListItem>();
                foreach (XmlNode listItem in listItemsXml.SelectNodes("rs:data/z:row", SPAdapter.Instance.NamespaceManager))
                {
                    SPItemItem item = new SPItemItem(listItem);
                    item.Location = new Point(0, y);
                    item.Size = new Size(width, 30);
                    item.Clicked += new EventHandler(delegate(object sender, EventArgs e)
                    {
                        SPItemItem it = (SPItemItem)sender;
                        if (it.ContentType == "Folder")
                        {
                            folders.Push(it.Name);
                            LoadData();
                        }
                        else
                        {
                            ViewItem viewItem = new ViewItem()
                            {
                                SiteUrl = this.SiteUrl,
                                ListId = this.ListId,
                                ListName = this.ListName,
                                ItemId = it.ItemId
                            };
                            viewItem.ShowDialog();
                        }
                    });
                    internalDataList.Add(item);
                    y += 30;
                }
                SlidingPanel_Height = Math.Max(y, height);
                SlidingPanel_TranslateY = 0;

                _IsDataLoading = false;
                Invoke(new UIThreadDelegate(HideWaitingCursor));
                Invoke(new UIThreadDelegate(CustomInvalidate));
            });
            asyncLoading.IsBackground = true;
            asyncLoading.Start();
        }

        private void ShowWaitingCursor()
        {
            Cursor.Current = Cursors.WaitCursor;
        }

        private void HideWaitingCursor()
        {
            Cursor.Current = Cursors.Default;
        }

        public void GoBack()
        {
            if (!AtRootFolder)
            {
                folders.Pop();
                LoadData();
            }
        }
    }
}
